/**
 * @author Szymon Pilkowski <szymon.pilkowski@gmail.com>
 * MIT license
 */


/*
 * pseudo-pointer object
 */


Pointer = {}

Pointer = function() {
		$("body").prepend("<div id='helperPointer' style='background:transparent url(pointer.gif) no-repeat;width:25px;height:25px;top:100px;left:100px;z-index:999;position:absolute'></div>")
		return $("#helperPointer");
};

/*
 * main object
 */


helper = {
	
	jump: 6,
	realclick: true,
	shouldplay: false,
	data: [],
	
	record: function(x,y,type) {
		this.data.push({x:x,y:y,type:type || "move"});
		
	},
	
	start: function() {
	    
		helper.data = [];
		
		$().mousemove( function(e) {
			
			helper.record(e.pageX,e.pageY)
			
		})
				
		$().mousedown( function(e) {
			
			helper.record(e.pageX,e.pageY,"mdown")
		})

		$().mouseup( function(e) {
			
			helper.record(e.pageX,e.pageY,"mup")
		})
		
		$().click( function(e) {
			
			helper.record(e.pageX,e.pageY,"click")
			
		})
		
	},
	
	singlestep: function() {

		typeof i != "undefined" ? i++ : i = 0;
		if ((i < steps) & (helper.shouldplay)) {
			if (helper.data[i].type == "move") {
				$(helperpointer).css({
					"left": helper.data[i].x,
					"top": helper.data[i].y
				})
			}
			else 
				if (helper.data[i].type == "mdown") {
					$(helperpointer).css("background-image", "url(pointer-red.gif)");
					$(helperpointer).css({
					"left": helper.data[i].x,
					"top": helper.data[i].y
				})
				}
				else 
					if (helper.data[i].type == "mup") {
					
						$(helperpointer).css("background-image", "url(pointer.gif)");
						$(helperpointer).css({
					"left": helper.data[i].x,
					"top": helper.data[i].y
				})
					} else if (helper.data[i].type == "click") {
						if (helper.realclick) {
							$(helperpointer).css({
								"left": helper.data[i].x,
								"top": helper.data[i].y
							})
							
							var evt = document.createEvent("MouseEvents");
							$(helperpointer).css("display", "none");
							var el = document.elementFromPoint(helper.data[i].x, helper.data[i].y);
							evt.initMouseEvent("click", true, true, window, 1, 0, 0, helper.data[i].x, helper.data[i].y, false, false, false, false, 0, null);
							el.dispatchEvent(evt);
							$(helperpointer).css("display", "block");
						} else {
							
							$(helperpointer).css("background-image", "url(/pointer-red.gif)");
							$(helperpointer).css({
								"left": helper.data[i].x,
								"top": helper.data[i].y
							})
							$(helperpointer).css("background-image", "url(/pointer.gif)");
						}
					}
					else {
						helper.stopplay();
					}
		}
		else {
			helper.stopplay();
		}

	},
	
	play: function() {
		i = 0;
		steps = helper.data.length;
		helperpointer = new Pointer();
		helper.shouldplay = true;
		if (typeof myTimer != "undefined") {
			clearInterval(myTimer)
		};
		myTimer = setInterval('helper.singlestep()',helper.jump)
	},
	
	stopplay: function() {
		$().unbind("mousemove");
		$().unbind("mousedown");
		$().unbind("mouseup");
		$().unbind("click");		
		helper.shouldplay = false;
		if (typeof myTimer != "undefined") {
			clearInterval(myTimer)
		};
		$("#helperPointer").remove();
		delete i;
	}
	
}
